#!/bin/bash

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    echo -e "${2}${1}${NC}"
}

print_header() {
    echo
    echo "========================================"
    print_message "   M3U8 Downloader 一键打包工具" "$CYAN"
    echo "========================================"
    echo
}

# 检查命令是否存在
command_exists() {
    command -v "$1" >/dev/null 2>&1
}

# 检查Node.js环境
check_nodejs() {
    print_message "检查Node.js环境..." "$YELLOW"
    
    if ! command_exists node; then
        print_message "❌ 错误: 未检测到Node.js，请先安装Node.js" "$RED"
        print_message "下载地址: https://nodejs.org/" "$BLUE"
        exit 1
    fi
    
    if ! command_exists npm; then
        print_message "❌ 错误: npm不可用，请检查Node.js安装" "$RED"
        exit 1
    fi
    
    print_message "✅ Node.js环境检查通过" "$GREEN"
    print_message "Node.js版本: $(node --version)" "$BLUE"
    print_message "npm版本: $(npm --version)" "$BLUE"
    echo
}

# 安装依赖
install_dependencies() {
    print_message "📦 正在安装依赖..." "$YELLOW"
    
    if npm install; then
        print_message "✅ 依赖安装完成" "$GREEN"
    else
        print_message "❌ 依赖安装失败" "$RED"
        exit 1
    fi
    echo
}

# 选择打包平台
select_platform() {
    echo "请选择打包平台:"
    echo "1. Windows"
    echo "2. macOS"
    echo "3. Linux"
    echo "4. 所有平台"
    echo "5. 仅打包前端"
    echo "6. 当前平台 (默认)"
    echo
    
    read -p "请输入选择 (1-6): " choice
    
    case $choice in
        1)
            print_message "🪟 开始打包Windows版本..." "$CYAN"
            npm run build:win
            ;;
        2)
            print_message "🍎 开始打包macOS版本..." "$CYAN"
            npm run build:mac
            ;;
        3)
            print_message "🐧 开始打包Linux版本..." "$CYAN"
            npm run build:linux
            ;;
        4)
            print_message "🌍 开始打包所有平台版本..." "$CYAN"
            npm run build:all
            ;;
        5)
            print_message "🌐 仅打包前端..." "$CYAN"
            npm run build:frontend
            ;;
        6|"")
            print_message "🖥️  开始打包当前平台版本..." "$CYAN"
            npm run build
            ;;
        *)
            print_message "🖥️  使用默认选项: 当前平台版本" "$YELLOW"
            npm run build
            ;;
    esac
}

# 显示构建结果
show_results() {
    if [ $? -eq 0 ]; then
        echo
        echo "========================================"
        print_message "✅ 打包完成！" "$GREEN"
        echo "========================================"
        echo
        print_message "📁 构建输出目录: build/" "$BLUE"
        echo
        
        print_message "可用的构建文件:" "$YELLOW"
        
        # 检查Windows文件
        if ls build/*.exe 1> /dev/null 2>&1; then
            print_message "  - Windows安装包 (.exe)" "$GREEN"
        fi
        
        # 检查macOS文件
        if ls build/*.dmg 1> /dev/null 2>&1; then
            print_message "  - macOS安装包 (.dmg)" "$GREEN"
        fi
        
        # 检查Linux文件
        if ls build/*.AppImage 1> /dev/null 2>&1; then
            print_message "  - Linux AppImage (.AppImage)" "$GREEN"
        fi
        
        if ls build/*.deb 1> /dev/null 2>&1; then
            print_message "  - Linux Debian包 (.deb)" "$GREEN"
        fi
        
        echo
        print_message "🎉 打包成功！可以分发这些文件了。" "$GREEN"
    else
        echo
        print_message "❌ 打包失败，请检查错误信息" "$RED"
        exit 1
    fi
}

# 主函数
main() {
    print_header
    check_nodejs
    install_dependencies
    select_platform
    show_results
}

# 运行主函数
main
